package com.example.gw_back_end.controller.admin;

import com.example.gw_back_end.domain.Result;
import com.example.gw_back_end.domain.dto.UsersTypeCount;
import com.example.gw_back_end.domain.entities.User;
import com.example.gw_back_end.service.GameService;
import com.example.gw_back_end.service.PostService;
import com.example.gw_back_end.service.UserService;
import com.example.gw_back_end.utils.ThreadLocalUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("admin")
public class AdminController {

    private final UserService userService;
    private final GameService gameService;
    private final PostService postService;
    public AdminController(UserService userService, GameService gameService, PostService postService) {
        this.userService = userService;
        this.gameService = gameService;
        this.postService = postService;
    }

    // 用户类型变更
    @PostMapping("account_type_change")
    public Result accountTypeUp(Integer userId,Integer accountType){
        Map<String,Object> claims = ThreadLocalUtil.get();
        Integer id = (Integer)claims.get("id");
        User admin = userService.getUserInfo(id);
        if (admin.getAccountType() == 3) {
            User user = userService.getUserInfo(userId);
            user.setAccountType(accountType);
            if (userService.updateInfo(user)) {
                return Result.success();
            } else {
                return Result.error();
            }
        } else {
            return Result.error("您尚未成为平台管理员，权限不足，操作失败！");
        }
    }

    // 设置用户账户状态
    @PostMapping("set_account_status")
    public Result setAccountStatus(Integer userId,Integer status){
        User user = userService.getUserInfo(userId);
        user.setStatus(status);
        user.setPassword(null);
        if (userService.updateInfo(user)) {
            return Result.success();
        } else {
            return Result.error();
        }
    }

    // 获取平台用户分类数总览
    @GetMapping("get_users_type_count")
    public Result getUsersTypeCount(){
        UsersTypeCount usersTypeCount = userService.getUsersTypeCount();
        if (usersTypeCount != null) {
            return Result.success(usersTypeCount);
        } else {
            return Result.error("数据获取失败，请稍后重试！");
        }
    }

    // 获取平台游戏总数
    @GetMapping("get_game_count")
    public Result getGameCount(){
        Integer gameCount = (int)gameService.count();
        return Result.success(gameCount);
    }

    // 获取平台帖子总数
    @GetMapping("get_post_count")
    public Result getPostCount(){
        Integer postCount = (int)postService.count();
        return Result.success(postCount);
    }

    // 平台管理员资格申请
//    @PostMapping("apply_admin")
//    public Result applyAdmin(Integer userId){
//        User user = userService.getUserInfo(userId);
//    }

}
